//
//  FHOAuthViewController.swift
//  WeiBo
//
//  Created by qfh on 16/7/10.
//  Copyright © 2016年 qiufuhe. All rights reserved.
//

import UIKit

import SVProgressHUD

let client_id: String = "475454112"

let client_secret: String = "8b7911d03167bc6ae83209f099b6c3d1"

let redirect_uri: String = "http://www.taobao.com"

class FHOAuthViewController: UIViewController {
    
    //webView 替换成当前view
   lazy private var webView = UIWebView()

    override func loadView() {
        
        //将视图替换成 webView
        
        webView.delegate = self
        
        view = webView
        
        webView.opaque = false
    }

    override func viewDidLoad() {
        super.viewDidLoad()
        
        view.backgroundColor = UIColor.whiteColor()
        
        //加载 navgationbar 设置
        loadNavgation()
        
        //加载网络
        
        requestOAithLogin()

        // Do any additional setup after loading the view.
    }
    
    /**
     加载navgationBar
     */
    func loadNavgation() {
    
        navigationItem.title = "微博登录"
        
        navigationItem.leftBarButtonItem = UIBarButtonItem(title: "取消", target: self, action: "dismAction")
        
        navigationItem.rightBarButtonItem = UIBarButtonItem(title: "自动填充", target: self, action: "fillAction")
    
    }
    
    // MARK: - 点击事件

    @objc private func dismAction() {
    
        SVProgressHUD.dismiss()
        
        dismissViewControllerAnimated(true, completion: nil)
        
    }
    
    @objc private func fillAction() {
        
//        print("自动填充密码")
        
    webView.stringByEvaluatingJavaScriptFromString("document.getElementById('userId').value = '1217014383@qq.com'; document.getElementById('passwd').value = '13027129214'")
    }
    
    
    // MARK: - 网络请求数据
    
    /**
    加载网页
    */
    func requestOAithLogin() {
    
    
        let url = "https://api.weibo.com/oauth2/authorize?client_id=\(client_id)&redirect_uri=\(redirect_uri)"
        
        let request = NSURLRequest(URL: NSURL(string: url)!)
        
        webView.loadRequest(request)
    
    }
    
       
 
    
}

extension FHOAuthViewController: UIWebViewDelegate {


    // MARK: - webView delegate
    func webViewDidStartLoad(webView: UIWebView) {
        
        SVProgressHUD.show()
    }
    
    
    
    func webViewDidFinishLoad(webView: UIWebView) {
        SVProgressHUD.dismiss()
        
    }
    
    //获取code
    func webView(webView: UIWebView, shouldStartLoadWithRequest request: NSURLRequest, navigationType: UIWebViewNavigationType) -> Bool {
        
        
        guard let url = request.URL else {
            
            return false
            
        }
        
        if !url.absoluteString.hasPrefix(redirect_uri) {
        
            return true
        
        }
        
        if let query = url.query where query.hasPrefix("code=")
        {
            
            /// 获取到code 值
            let code = query.substringFromIndex("code=".endIndex)
            
            
            FHUserAccessViewModel.sharedUserAccessViewModel.requesAccess_token(code, cellBack: { (isRequest) -> () in
                
                if isRequest {
                    
                    print("登录成功")
                    
                    NSNotificationCenter.defaultCenter().postNotificationName(SwitchRootVCNotification, object: self)
                    
                }else {
                    print("登录失败")
                }
                
            })
            
//            requesAccess_token(code)
    
        } else {
            dismissViewControllerAnimated(true, completion: nil)
        }
        
        
       
        
        
        
        return false
    }


}


